#include <pthread.h>

#include "SmartMediaPlayer.h"

void* MessageReceive(void* data);

SmartMediaPlayer::SmartMediaPlayer()
{
	mQueue = new MsgQueue();
	status_t ret = pthread_create(&mThread, NULL, ThreadFunc, (void *)this);
}

SmartMediaPlayer::~SmartMediaPlayer()
{
	if(NULL != mQueue)
	{
		Msg_T msg;
		msg.type = ShutDownOwn;
		mQueue->SendMessage(&msg);

		pthread_join(mThread, NULL);

		delete mQueue;
		mQueue = NULL;
	}

}

/*
void SmartMediaPlayer::setDriver(SmartMediaPlayerDriver* driver)
{
	mDriver = driver;
}
*/

void SmartMediaPlayer::serDataSourceAsync(const char* url)
{
	if(NULL != mQueue)
	{
		Msg_T msg;
		msg.type = SetDataSource;
		mQueue->SendMessage(&msg);
	}
}

void* SmartMediaPlayer::MessageReceive()
{
	Msg_T msg;

	while(1)
	{
		mQueue->ReceiveMessage(&msg);
		switch(msg.type)
		{
			case ShutDownOwn:
			{
				return NULL;
			}
			case SetDataSource:
			{
				//mDriver->notifySetDataSourceCompleted(msg.err);
				break;
			}
			default:
				break;
		}
	}
}
void* SmartMediaPlayer::ThreadFunc(void* data)
{
	return (void *)((SmartMediaPlayer *)data)->MessageReceive();
}